The question lingering on your mind all week

Apricots

@emiliano.neroni

Anyway,
Functional

Imperative /
Procedural
Functional
Object-Oriented

Procedural

10 PRINT "What is your name?"
20 k$ = INKEY$
30 PRINT k$
40 LET total = 0
50 FOR counter = 0 TO 99
60     total = total + counter
70 NEXT counter
80 PRINT total
25 PRINT "Hello"

Functional

alert("Hello" + prompt("What is your name?"));
alert(
  Array
    .from(Array(100).keys())
    .reduce(
      (total, value, i) => (total + i),
      0
    )
);

Arrays

[1,2,3,4].filter( val => (val > 2) )

[1,2,3,4].map( val => (val + val) )

[1,2,3,4].reduce(
  (accumulator, val) => (accumulator + val),
  0,
)

Objects

const obj = { a:1, b:2 };

const keys = Object.keys(obj);

const filteredKeys = keys.filter(
  key => ( obj[key] > 1 )
);

const filteredObj = filteredKeys.reduce(
  (accumulator, key) => {
    accumulator[key] = obj[key];
    return accumulator;
  },
  {},
);

All together now

const obj = { a:1, b:2 };

const filteredObj = Object
  .keys(obj)
  .filter( key => ( obj[key] > 1 ) )
  .reduce(
    (accumulator, key) => {
      accumulator[key] = obj[key];
      return accumulator;
    },
    {},
  );

Functional

"Pure Functions"

"No Side Effects"

Immutable and Stateless.

"Currying"

Helpers

Ramda.js

R.add() and R.ap() examples

Final word

Speed

Conclusion

Functions are easier to test, to mock

They will be fast soon

Typing

PropTypes

TypeScript or Flow

// @flow

const getUsers = (users: any, index: number) => {
  return users[id];
};

const users = [{name: 'Matthew'}];
// args wrong!
getUsers(0, users);

Typing

type User = { name: string }
type Users = Array<User>

const renderUsers = (users: Users, index: number) => {
  return users[index];
};

renderUsers( {name: 'Matt', other: false }, 0);
function bigNumbersRcool(num: number) {
  if (num > 10) {
    return 'cool';
  }
}

const result: string = bigNumbersRcool(100);
console.log(result.toString());

console.log(bigNumbersRcool(1).toString());

// TypeScript:
// error at runtime
// "Cannot read property 'toString' of undefined"

// Flow:
// error: call of method `toString`.
// Method cannot be called on possibly null value

(credit)

Conclusion

Use typing on long term or big projects